package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"github.com/jinzhu/gorm"
	"house/models"
)

type MainController struct {
	beego.Controller
}

type Res struct {
	Cre  int
	Msg  string
	Data interface{}
}

func (c *MainController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	var data []models.Store
	models.DB.Find(&data)

	c.Data["json"] = Res{200, "列表查询成功", data}
	c.ServeJSON(true)
}
func (c *MainController) Add() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	StoreName := c.GetString("StoreName")
	StoreManager := c.GetString("StoreManager")
	StoreAddress := c.GetString("StoreAddress")
	ShowAddress := c.GetString("ShowAddress")

	data := models.Store{
		Model:        gorm.Model{},
		StoreName:    StoreName,
		StoreManager: StoreManager,
		StoreAddress: StoreAddress,
		ShowAddress:  ShowAddress,
	}
	models.DB.Create(&data)
	c.Data["json"] = Res{200, "添加成功", data}
	c.ServeJSON(true)
}

func (c *MainController) Update() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	id, _ := c.GetInt("id")
	StoreName := c.GetString("StoreName")
	StoreManager := c.GetString("StoreManager")
	StoreAddress := c.GetString("StoreAddress")
	ShowAddress := c.GetString("ShowAddress")

	data := models.Store{
		Model:        gorm.Model{ID: uint(id)},
		StoreName:    StoreName,
		StoreManager: StoreManager,
		StoreAddress: StoreAddress,
		ShowAddress:  ShowAddress,
	}
	models.DB.Model(&data).Updates(&data)
	c.Data["json"] = Res{200, "修改成功", data}
	c.ServeJSON(true)
}

func (c *MainController) Show() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	id, _ := c.GetInt("id")
	data := models.Store{
		Model: gorm.Model{ID: uint(id)},
	}
	models.DB.Find(&data)
	c.Data["json"] = Res{200, "详情查询成功", data}
	c.ServeJSON(true)
}
